package org.dromara.wms.service;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.dromara.common.mybatis.core.page.PageQuery;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.dromara.common.web.core.BaseService;
import org.springframework.stereotype.Service;
import org.dromara.wms.domain.WmsSchedule;
import org.dromara.wms.mapper.WmsScheduleMapper;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 生产计划排程Service业务层处理
 *
 * @author wenrui Su
 * @date 2024-05-05
 */
@RequiredArgsConstructor
@Service
public class WmsScheduleService extends BaseService {

    private final WmsScheduleMapper baseMapper;

    /**
     * 查询生产计划排程
     */
    public WmsSchedule queryById(Long id){
        return baseMapper.selectById(id);
    }

    /**
     * 查询生产计划排程列表
     */
    public TableDataInfo<WmsSchedule> queryPageList(WmsSchedule bo, PageQuery pageQuery) {
      Page<WmsSchedule> result = baseMapper.selectLine(bo, pageQuery.build());
      return TableDataInfo.build(result);
    }

    /**
     * 查询生产计划排程列表
     */
    public List<WmsSchedule> queryList(WmsSchedule bo) {
        LambdaQueryWrapper<WmsSchedule> lqw = buildQueryWrapper(bo);
        return baseMapper.selectList(lqw);
    }

    private LambdaQueryWrapper<WmsSchedule> buildQueryWrapper(WmsSchedule bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<WmsSchedule> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getOrderId() != null, WmsSchedule::getOrderId, bo.getOrderId());
        lqw.eq(bo.getProductId() != null, WmsSchedule::getProductId, bo.getProductId());
        lqw.eq(bo.getDeliveryTime() != null, WmsSchedule::getDeliveryTime, bo.getDeliveryTime());
        return lqw;
    }

    /**
     * 新增生产计划排程
     */
    public Boolean insertByBo(WmsSchedule bo) {
        validEntityBeforeSave(bo);
        boolean flag = baseMapper.insert(bo) > 0;
        return flag;
    }

    /**
     * 修改生产计划排程
     */
    public Boolean updateByBo(WmsSchedule bo) {
        validEntityBeforeSave(bo);
        return baseMapper.updateById(bo) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(WmsSchedule entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除生产计划排程
     */
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
